package cn.getech.data.development.utils;

import cn.getech.data.development.dto.HBaseDto;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.AsyncConnection;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName EsUtil
 * @Description TODO
 * @Author Getech
 * @Date 2021/1/11 10:26
 */
public class HBaseUtil {

    /**
    * @Description  获取Hbaase连接客户端
    * @Author  chengweiping
    * @Date   2021/1/11 10:45
    */
    public static AsyncConnection getConnection(HBaseDto hBaseDto){
        AsyncConnection connection =null;
        try {
            // 取得一个数据库连接的配置参数对象
            Configuration conf = HBaseConfiguration.create();
            String ip=hBaseDto.getIp();
            String port=hBaseDto.getPort();
            // 设置连接参数：HBase数据库所在的主机IP
            conf.set("hbase.zookeeper.quorum", ip);
            // 设置连接参数：HBase数据库使用的端口
            conf.set("hbase.zookeeper.property.clientPort", port);
            conf.set("HADOOP_USER_NAME","hbase");

            // 取得一个数据库连接对象
            CompletableFuture<AsyncConnection> connectionCompletableFuture=ConnectionFactory.createAsyncConnection(conf);
            connection=connectionCompletableFuture.get(10, TimeUnit.SECONDS);
            System.out.println("创建hbase连接成功");


        } catch (Exception e) {
            e.printStackTrace();
            connection=null;
        }
        return connection;
    }



}
